// date.js
export function formatDate(date, fmt) {
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(
      RegExp.$1,
      (date.getFullYear() + "").substr(4 - RegExp.$1.length)
    );
  }
  let o = {
    "M+": date.getMonth() + 1,
    "d+": date.getDate(),
    "h+": date.getHours(),
    "m+": date.getMinutes(),
    "s+": date.getSeconds(),
  };
  for (let k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      let str = o[k] + "";
      fmt = fmt.replace(
        RegExp.$1,
        RegExp.$1.length === 1 ? str : padLeftZero(str)
      );
    }
  }
  return fmt;
}


function repair0(m){
	return m<10?'0'+m:m 
	}

export function disposeDate(str) {
  const timestamp=new Date(str).getTime()
  const time = new Date(timestamp);
  const y = time.getFullYear();
  const m = time.getMonth() + 1;
  const d = time.getDate();
  const h = time.getHours();
  const mm = time.getMinutes();
  const s = time.getSeconds();
  return (
    y +
    "-" +
    repair0(m) +
    "-" +
    repair0(d) +
    " " +
    repair0(h) +
    ":" +
    repair0(mm) +
    ":" +
    repair0(s)
  );
}

function padLeftZero(str) {
  return ("00" + str).substr(str.length);
}

export function str2Date(dateStr, separator) {
  if (!separator) {
    separator = "-";
  }
  let dateArr = dateStr.split(separator);
  let year = parseInt(dateArr[0]);
  let month;
  //处理月份为04这样的情况
  if (dateArr[1].indexOf("0") == 0) {
    month = parseInt(dateArr[1].substring(1));
  } else {
    month = parseInt(dateArr[1]);
  }
  let day = parseInt(dateArr[2]);
  let date = new Date(year, month - 1, day);
  return date;
}

export function time_To_hhmmss(seconds) {
  var hh;
  var mm;
  var ss;

  if (seconds == null || seconds <= 0) {
    return "00:00:00";
  }
  hh = (seconds / 3600) | 0;
  seconds = parseInt(seconds) - hh * 3600;
  if (parseInt(hh) < 10) {
    hh = "0" + hh;
  }

  mm = (seconds / 60) | 0;

  ss = parseInt(seconds) - mm * 60;
  if (parseInt(mm) < 10) {
    mm = "0" + mm;
  }
  if (ss < 10) {
    ss = "0" + ss;
  }
  return hh + ":" + mm + ":" + ss;
}

export function dealDate(startTime, endTime) {
  console.log(startTime, endTime);
  let groupDate = [];
  let s_1 = formatDate(new Date(startTime), "yyyy-MM-dd");
  let s = new Date(s_1);
  groupDate.push(s_1);
  s_1 = null;
  let e = new Date(formatDate(new Date(endTime), "yyyy-MM-dd"));

  let distanceDayLength = (e.getTime() - s.getTime()) / (1000 * 60 * 60 * 24);
  //   console.log(distanceDayLength)
  let startDay = s;

  for (let i = 0; i < distanceDayLength; i++) {
    let t = formatDate(
      new Date((new Date(startDay) / 1000 + 86400) * 1000),
      "yyyy-MM-dd"
    );
    startDay = t;
    groupDate.push(t);
  }
  return groupDate;
}
